<?php
namespace Home\Controller;
use Common\Controller\HomeBaseController;
/**
 * 商城首页Controller
 */
class AdminController extends HomeBaseController{

    public function _initialize(){



    }

	/**
	 * 首页
	 */
	public function index(){

        $keycode = substr(md5(date("Ymd",time())."Yumcms.org"),0,16);

        if(IS_POST){

            $data = D('Admin')->login($keycode);

            if($data['code'] == 200){

                $this->redirect('Admin/Index/index');

            }elseif($data['code'] == 403){

                $this->redirect('Home/Admin/activation_mail');

            }{

                $this->error($data['msg']);
            }

        }else{

            if(check_login()){

                $this->success('登录成功、前往管理后台',U('Admin/Index/index'));

            }else{
                $this->assign("keycode",$keycode);
                $this->display();
            }
        }
	}

    /**
     * 用户注册
     */
    public function register(){

        $keycode = substr(md5(date("Ymd",time())."Yumcms.org"),0,16);

        if(IS_POST){

            //获取邮件发送模板
            $mail_template = $this->fetch("./tpl/Home/Admin/mail_template.html");

            $data = D('Admin')->register($keycode,$mail_template);

            if($data['code'] == 200){

                $this->redirect('Home/Admin/activation_mail');

            }else{
                $this->error($data['msg']);
            }

        }else{

            $this->assign("keycode",$keycode);
            $this->display();
        }
    }

    /**
     * 验证重复注册用户
     *
     */
    public function validation(){

        $data = D('Admin')->validation();

        $this->ajaxReturn($data);

    }

    /**
     * 忘记密码
     */
    public function forgot_password(){

        if(IS_POST){

            //获取邮件发送模板
            $password_template = $this->fetch("./tpl/Home/Admin/password_template.html");

            $data = D('Admin')->forgot_password($password_template);

            if($data['code'] == 200){
                $this->assign($data);
                $this->display('success_password');
            }else{
                $this->display('fail_password');
            }

        }else{

            $this->display();
        }

    }

    /**
     * 激活账号
     */
    public function activation(){

        $data = D('Admin')->activation();

        $this->assign("msg",$data['msg']);

        if($data['code'] == 200){

            $this->display();

        }else{

            $this->display('activation_error');

        }

    }

    /**
     * 激活账号 - 邮箱
     */
    public function activation_mail(){

        $email = session('user.email');

        if(!empty($email)){

            $rule = "/([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?$/i";

            preg_match($rule,$email,$return);

            $links = "mail.".$return[0];

            $this->assign("links",$links);
            $this->assign("email",$email);
            $this->display();

        }else{

            $this->redirect('Home/Admin/activation_fail');

        }


    }

    /**
     * 激活邮件发送
     */
    public function send_mail(){

        //获取邮件发送模板
        $mail_template = $this->fetch("./tpl/Home/Admin/mail_template.html");

        $data = D('Admin')->send_mail($mail_template);

        $this->ajaxReturn($data);

    }

    /**
     * 重新发送找回密码
     */
    public function reset_password(){

        //获取邮件发送模板
        $password_template = $this->fetch("./tpl/Home/Admin/password_template.html");

        $data = D('Admin')->reset_password($password_template);

        $this->ajaxReturn($data);

    }

    /**
     * 激活验证失败
     */
    public function activation_fail(){


        $this->display();

    }

    /**
     * 重设密码
     */
    public function rebuild_password(){

        $keycode = substr(md5(date("Ymd",time())."Yumcms.org"),0,16);

        if(IS_POST){

            $data = D('Admin')->rebuild_password($keycode);

            if($data['code'] == 200){
                $this->display('success_rebuild');
            }else{
                $this->display('rebuild_password_error');
            }

        }else{

            preg_match('/rebuild_password\/([0-9]*)\/([0-9a-zA-z]*)\.html/is',$_SERVER['REQUEST_URI'],$link);

            $where['id'] = array("eq",$link[1]);

            $where['email_code'] = array("eq",$link[2]);

            $user_info = M("users")->field('id,email_code')->where($where)->find();

            if(!empty($user_info)){

                $data = array("user_id"=>$user_info['id'],"key"=>$user_info['email_code'],"keycode"=>$keycode);

                $this->assign($data);
                $this->display();
            }else{
                $this->display('rebuild_password_error');
            }
        }

    }

    /**
     * 修改邮箱
     */
    public function confirm_email(){

        $data = D('Admin')->confirm_email();

        if($data['code'] == 200){

            $this->display();
        }else{
            $this->display('rebuild_password_error');
        }

    }


    /**
     * 退出
     */
    public function logout(){
        session('user',null);
        $this->success('退出成功、前往登录页面',U('Home/Admin/index'));
    }


}

